package com.szh.mysobproject.ui.adapter

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.text.*
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.URLSpan
import android.view.*
import android.view.textclassifier.TextLinks.TextLinkSpan
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.core.text.HtmlCompat
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import com.szh.mysobproject.R
import com.szh.mysobproject.databinding.SystemMsgListItemBinding
import com.szh.mysobproject.model.msg.SystemMsg
import com.szh.mysobproject.ui.activity.BrowserActivity
import com.szh.mysobproject.util.setFixOnClickListener
import timber.log.Timber


/**
 * @author : create by  szh
 * @date : 2022/7/12 16:30
 * @desc :系统消息列表 适配器
 */

class SystemMsgAdapter(private val adapterDelegate: AdapterDelegate) :
    PagingDataAdapter<SystemMsg.Content, SystemMsgAdapter.SystemMsgVieHolder>(
        SystemMsgDiffCallback()
    ) {

    private lateinit var context: Context

    class SystemMsgDiffCallback : DiffUtil.ItemCallback<SystemMsg.Content>() {
        override fun areItemsTheSame(
            oldItem: SystemMsg.Content,
            newItem: SystemMsg.Content
        ): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: SystemMsg.Content,
            newItem: SystemMsg.Content
        ): Boolean {
            return oldItem == newItem
        }
    }

    inner class SystemMsgVieHolder(val binding: SystemMsgListItemBinding) :
        RecyclerView.ViewHolder(binding.root)

    override fun onViewAttachedToWindow(holder: SystemMsgVieHolder) {
        super.onViewAttachedToWindow(holder)
        adapterDelegate.onViewAttachedToWindow(holder)
    }

    override fun onBindViewHolder(holder: SystemMsgVieHolder, position: Int) {
        val itemView = holder.itemView
        val binding = holder.binding
        val ivAvatar = binding.ivAvatar
        val cbNickName = binding.cbNickName
        val tvDesc = binding.tvDesc
        val tvReplyMsg = binding.tvReplyMsg
        val tvChildReplyMsg = binding.tvChildReplyMsg
        context = itemView.context
        val item = getItem(position) ?: return
        itemView.setFixOnClickListener {
            adapterDelegate.onItemClick(it, position)
        }
        Glide.with(itemView)
            .load(R.mipmap.ic_gold)
            .placeholder(R.mipmap.ic_gold)
            .error(R.mipmap.ic_gold)
            .circleCrop()
            .into(ivAvatar)
        cbNickName.text = item.title
        val sdf = TimeUtils.getSafeDateFormat("yyyy-MM-dd HH:mm")
        tvDesc.text = TimeUtils.getFriendlyTimeSpanByNow(item.publishTime, sdf)
        tvReplyMsg.height = 0
//        tvChildReplyMsg.text = HtmlCompat.fromHtml(item.content, HtmlCompat.FROM_HTML_MODE_LEGACY)
        val spanned = HtmlCompat.fromHtml(item.content, HtmlCompat.FROM_HTML_MODE_LEGACY)
        val spannableStringBuilder = SpannableStringBuilder(spanned)
        val spans = spannableStringBuilder.getSpans(
            0, spanned.length,
            URLSpan::class.java
        )
        for (span in spans) {
            //点击事件
            setClick(spannableStringBuilder, span)
            spannableStringBuilder.removeSpan(span)//如果不删除 点击事件会失效  原因：    ClickableSpan link = links[0];
        }
        tvChildReplyMsg.text = spannableStringBuilder
        tvChildReplyMsg.movementMethod = LinkMovementMethod.getInstance()
    }

    private fun setClick(spannableStringBuilder: SpannableStringBuilder, span: URLSpan) {
        val spanStart = spannableStringBuilder.getSpanStart(span)
        val spanEnd = spannableStringBuilder.getSpanEnd(span)
        val flags = spannableStringBuilder.getSpanEnd(span)

        val clickableSpan: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                Timber.d("根据URL跳转到对应的网页上")
                val url = span.url
                BrowserActivity.start(context, url)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                //不显示下划线
                Timber.d("不显示下划线")
                ds.isUnderlineText = false
            }
        }
        spannableStringBuilder.setSpan(clickableSpan, spanStart, spanEnd, flags)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SystemMsgVieHolder {
        val binding =
            SystemMsgListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return SystemMsgVieHolder(binding)
    }
}